#!/usr/bin/python
#-*-coding: utf-8 -*-

########## DO NOT CHANGE THESE LINES ###########################################
import sys
from pylot.core import (ENCODING, Path)
try : __file__
except NameError : co = sys._getframe(0).f_code ; __file__ = co.co_filename
ufile = __file__.decode(ENCODING)
if ufile.strip() : PLUGIN_PATH = Path(ufile).abspath().parent
else : PLUGIN_PATH = Path(u'.').abspath()
########## DO NOT CHANGE THESE LINES ###########################################

from PyQt4 import QtGui
from PyQt4.QtCore import SIGNAL


from pylot.core import (check_type, Datum, DirPath)
from pylot.qtgui import (load_designer_file, GuiAutoWidget,
                                 GuiPathChooser, GuiUnicodeChooser)

from handler import HandlerPart

_GuiPart, WidgetClass = load_designer_file(u'designer.ui', [PLUGIN_PATH])
class GuiPart(WidgetClass, GuiAutoWidget, _GuiPart):
  u"""
  Translation tool
  """
  def __init__(self, handler=None):
    if handler is None :
      handler = HandlerPart()
    GuiAutoWidget.__init__(self, handler)
    self._plugin_path = PLUGIN_PATH

    WidgetClass.__init__(self)
    self.setupUi(self)

    datum_path = self.handler.opt[u'lastpaths']['plugins']
    datum_lang = self.handler.opt[u'translation']['lang']

    self.widget_path = GuiPathChooser(datum_path, handler=self.handler)
    self.widget_lang = GuiUnicodeChooser(datum_lang, handler=self.handler)

    self.addpyLotWidget(self.widget_path)
    self.addpyLotWidget(self.widget_lang)

    self.grid_layout.addWidget(self.widget_path, 0, 1)
    self.grid_layout.addWidget(self.widget_lang, 1, 0, 1, 2)

    self._createActions()
    self._createConnexions()
    self._createMenus()
    self._createToolBars()

  def _createConnexions(self):
    # action for i18n (special GUI treatments)
    self.connect(self.action_init_translation, SIGNAL('triggered()'),
                 self._onActionInitTranslationTriggered)

    # action for i18n (direct calls to handler)
    self.connect(self.action_compile_translation, SIGNAL('triggered()'),
                 self.handler.actionCompileTranslation)
    self.connect(self.action_update_translation, SIGNAL('triggered()'),
                 self.handler.actionUpdateTranslation)
    self.connect(self.action_update_pylot_translation, SIGNAL('triggered()'),
                 self.handler.actionUpdatepyLotTranslation)
    self.connect(self.action_compile_pylot_translation, SIGNAL('triggered()'),
                 self.handler.actionCompilepyLotTranslation)

    # GUI only actions
    self.connect(self.action_open, SIGNAL('triggered()'),
                 self.widget_path.chooseDir)

  def _createMenus(self):
    self.menu_file = QtGui.QMenu()
    self.menu_file.setObjectName(u'menu_file')
    self.addMenu_(self.menu_file)

    self.menu_translate = QtGui.QMenu()
    self.menu_translate.setObjectName(u'menu_translate')
    self.addMenu_(self.menu_translate)

  def _fillMenus(self, level):
    self._fill(self.menu_translate, [
      self.action_init_translation,
      u'-',
      self.action_update_translation,
      self.action_compile_translation,
      u'-',
      (self.action_update_pylot_translation, u'devel'),
      (self.action_compile_pylot_translation, u'devel')
      ])

    self._fill(self.menu_file, [
      self.action_open,
      ])

  def _createToolBars(self):
    self.tool_bar_translate = QtGui.QToolBar()
    self.tool_bar_translate.setObjectName(u'ToolBarTranslate')
    self.addToolBar_(self.tool_bar_translate)

  def _fillToolBars(self, level):
    self.tool_bar_translate.clear()
    self._fill(self.tool_bar_translate, [
      self.action_init_translation,
      u'-',
      self.action_update_translation,
      self.action_compile_translation,
      ])

  def _decorate (self):
    self._setIcons([
      (self.action_init_translation, u'init_translation.png'),
      (self.action_compile_translation, u'compile_translation.png'),
      (self.action_update_translation, u'update_translation.png'),
      (self.action_compile_pylot_translation, u'compile_translation.png'),
      (self.action_update_pylot_translation, u'update_translation.png'),
      ])

  def _translate(self):
    self._setTexts([
      (self, self.tr_(u'I18N tools')),
      (self.tool_bar_translate, self.tr_(u'Translate ToolBar')),
      (self.menu_translate, self.tr_(u'Translate')),
      (self.menu_file, self.tr_(u'File'))
      ])

  def _onPluginPathChanged(self, datum, value):
    self.widget_path.updateData()
    self.pref.refresh()

  def _onPrefPathChanged(self):
    self.pref.updateData()
    self.widget_path.refresh()

  def _onActionInitTranslationTriggered(self):
    ret = QtGui.QMessageBox.critical(self, self.tr_(u"Confirm initialization"),
       self.tr_(u"Initializing translation will replace existing translations.\nContinue ?"),
       QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
    if ret == QtGui.QMessageBox.Ok :
      self.handler.initTranslation(self.widget_path.getValue())

if __name__ == u"__main__":
  pass
